#include "chatbox.h"
#include <QScrollBar>
#include <QDateTime>

ChatBox::ChatBox(QWidget *parent): QListWidget(parent)
{
    // this->setParent(parent);  // 确保它有一个父对象
    this->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    this->verticalScrollBar()->setSingleStep(5);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}


void ChatBox::insertNewChatMessage(QString text, QString time, bool isSend)
{
    dealMessageTime(time);
    ChatMessageBubble* messageW = new ChatMessageBubble(this);
    QListWidgetItem* item = new QListWidgetItem(this);
    if (isSend)
    {
        dealMessage(messageW, item, text, time, ChatMessageBubble::User_Me);
    }
    else
    {
        dealMessage(messageW, item, text, time, ChatMessageBubble::User_She);
    }
    // 设置当前显示的为最新一行
    this->setCurrentRow(this->count()-1);
}

void ChatBox::refreshUI()
{
    for(int i = 0; i < this->count(); i++)
    {
        ChatMessageBubble* messageW = (ChatMessageBubble*)this->itemWidget(this->item(i));
        QListWidgetItem* item = this->item(i);
        dealMessage(messageW, item, messageW->text(), messageW->time(), messageW->userType());
    }
    // 设置当前显示的为最新一行
    this->setCurrentRow(this->count()-1);
}

void ChatBox::dealMessage(ChatMessageBubble* chatMessageBox, QListWidgetItem* item, QString text,
    QString time,  ChatMessageBubble::User_Type type)
{
    chatMessageBox->setFixedWidth(this->width());
    QSize size = chatMessageBox->fontRect(text);
    item->setSizeHint(size);
    chatMessageBox->setText(text, time, size, type);
    this->setItemWidget(item, chatMessageBox);
}

void ChatBox::dealMessageTime(QString curMsgTime)
{
    qDebug() << "curMsgTime: " << curMsgTime;
    QDateTime curDateTimeObj = QDateTime::fromString(curMsgTime, Qt::ISODate);
    qDebug() << "current QDateTime object: " << curDateTimeObj;
    bool isShowTime = false;
    // 获取聊天框
    if(this->count() > 0)
    {
        QListWidgetItem* lastItem = this->item(this->count() - 1);
        ChatMessageBubble* messageW = (ChatMessageBubble*)this->itemWidget(lastItem);
        QString lastTimeStr = messageW->time();
        qDebug() << "last Time string: " << lastTimeStr;
        QDateTime lastDateTimeObj = QDateTime::fromString(lastTimeStr, Qt::ISODate);
        int inteval = qAbs(curDateTimeObj.secsTo(lastDateTimeObj));
        qDebug() << "time interval:" << inteval;
        isShowTime = (inteval > 180); // 两个消息相差一分钟
        // isShowTime = true;
    }
    else
    {
        isShowTime = true;
    }
    if(isShowTime)
    {
        ChatMessageBubble* messageTime = new ChatMessageBubble(this);
        qDebug() << "show current time .......";
        QListWidgetItem* itemTime = new QListWidgetItem(this);
        // QListWidgetItem* itemTime2 = new QListWidgetItem();
        QSize size = QSize(this->width(), 40);
        messageTime->resize(size);
        itemTime->setSizeHint(size);
        messageTime->setText(curMsgTime, curMsgTime, size, ChatMessageBubble::User_Time);
        this->setItemWidget(itemTime, messageTime);
    }
}

void ChatBox::resizeEvent(QResizeEvent *event)
{
    // this->setGeometry(QRect(0, this->y(), this->width(), this->height()));
    // for(int i = 0; i < this->count(); i++)
    // {
    //     ChatMessageBubble* messageW = (ChatMessageBubble*)this->itemWidget(this->item(i));
    //     QListWidgetItem* item = this->item(i);
    //     dealMessage(messageW, item, messageW->text(), messageW->time(), messageW->userType());
    // }
    // QListWidget::resizeEvent(event);  // 继续处理 resizeEvent
}


